﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 02/04/2014
 * Time: 02:38 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data;
using System.Data.OleDb;
using Gestión_UTN.Entities;

namespace Gestión_UTN.daimpl
{
    /// <summary>
    /// Description of carreradaimpl.
    /// </summary>
    public class carreradaimpl : Gestión_UTN.da.carrerada
    {
        public carreradaimpl()
        {
        }

        public bool SaveCarrera(string nombre)
        {
            bool ingresada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "INSERT INTO car_carreras(car_nombre)" +
                                "VALUES(@nombre)";

                    conex.Open();
                    trans = conex.BeginTransaction();

                    OleDbCommand insertar = new OleDbCommand(sql, conex, trans);

                    insertar.Parameters.AddWithValue("@nombre", nombre);

                    insertar.ExecuteNonQuery();

                    trans.Commit();

                    ingresada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                ingresada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return ingresada;
        }

        public bool DeleteCarrera(string nombre)
        {
            bool eliminada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "DELETE FROM car_carreras " +
                        "WHERE car_nombre = @nombre";

                    conex.Open();

                    trans = conex.BeginTransaction();

                    OleDbCommand eliminar = new OleDbCommand(sql, conex, trans);

                    eliminar.Parameters.AddWithValue("@nombre", nombre);

                    eliminar.ExecuteNonQuery();

                    trans.Commit();

                    eliminada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                eliminada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return eliminada;
        }

        public bool ModifyCarrera(string nombre_anterior, string nombre)
        {
            bool modificada = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "UPDATE car_carreras SET car_nombre = @nombre " +
                                    "WHERE car_nombre = @nombre_anterior";

                    conex.Open();
                    trans = conex.BeginTransaction();

                    OleDbCommand modificar = new OleDbCommand(sql, conex, trans);

                    modificar.Parameters.AddWithValue("@nombre", nombre);
                    modificar.Parameters.AddWithValue("@nombre_anterior", nombre_anterior);

                    modificar.ExecuteNonQuery();

                    trans.Commit();

                    modificada = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                modificada = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return modificada;
        }

        public System.Data.DataTable FindAllCarreras()
        {
            DataTable tablaCarreras = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                tablaCarreras = new DataTable();

                if (null != conex)
                {

                    string sql = "SELECT car_id, car_nombre" +
                                    " FROM car_carreras car";

                    OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);

                    conex.Open();

                    adaptador.Fill(tablaCarreras);

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                tablaCarreras = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return tablaCarreras;
        }

        public Gestión_UTN.Entities.Carrera FindCarreraByNombre(string nombre)
        {
            Carrera carrera = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();

                if (null != conex)
                {

                    string sql = "SELECT car_id, car_nombre" +
                                    " FROM car_carreras car " +
                                    "WHERE car_nombre = @car_nombre";

                    OleDbCommand consulta = new OleDbCommand(sql, conex);

                    conex.Open();

                    consulta.Parameters.AddWithValue("@car_nombre", nombre);

                    OleDbDataReader lector = consulta.ExecuteReader();

                    while (lector.Read())
                    {
                        int car_id = Convert.ToInt32(lector[0].ToString());
                        string nom = lector[1].ToString();

                        carrera = new Carrera(car_id, nom);

                    }

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                carrera = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return carrera;
        }

        public Carrera FindCarreraById(int id)
        {
            Carrera carrera = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();

                if (null != conex)
                {

                    string sql = "SELECT car_id, car_nombre" +
                                    " FROM car_carreras car " +
                                    "WHERE car_id = @car_id";

                    OleDbCommand consulta = new OleDbCommand(sql, conex);

                    conex.Open();

                    consulta.Parameters.AddWithValue("@car_id", id);

                    OleDbDataReader lector = consulta.ExecuteReader();

                    while (lector.Read())
                    {
                        int car_id = Convert.ToInt32(lector[0].ToString());
                        string nom = lector[1].ToString();

                        carrera = new Carrera(car_id, nom);

                    }

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                carrera = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return carrera;
        }
    }
}
